/**
 * 
 */
package com.zhaoyan.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bot.utils.NumberUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhaoyan.mapper.VoteMapper;
import com.zhaoyan.pojo.VoteItem;
import com.zhaoyan.pojo.VoteTopic;
import com.zhaoyan.service.VoteService;

/**
 * @author zhaoyan
 *
 * 2020年1月30日
 */
@Service
public class VoteServiceImpl implements VoteService{
	
	
	@Autowired
	private VoteMapper voteMapper;

	@Override
	public PageInfo<VoteTopic> showTopic(Integer pageNum) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, 1);
		
		PageInfo<VoteTopic> pageInfo = new PageInfo<VoteTopic>(voteMapper.showTopic());
		
		
		//	设置百分比
		setPro(pageInfo);
		
		return pageInfo;
	} 
		
	private void setPro(PageInfo<VoteTopic> pageInfo){
		
		List<VoteTopic> list = pageInfo.getList();
		
		for (VoteTopic voteTopic : list) {
			
			Integer num = 0;
			
			List<VoteItem> itemList = voteTopic.getList();
			
			for (VoteItem item : itemList) {
				
				Integer node = item.getVotes();
				
				num+=node;			
			}
			
			voteTopic.setTotal(num);
			
		}
		
		//	计算百分比
		for (VoteTopic voteTopic : list) {
			
			List<VoteItem> itemList = voteTopic.getList();
			
			for (VoteItem item : itemList) {
				
				Integer node = item.getVotes();
				
				int percent = NumberUtil.getPercent(node, voteTopic.getTotal());	
				
				String msg = "<div class=\"progress\">\r\n" + 
						"<div class=\"progress-bar\" role=\"progressbar\" style=\'width:"+percent+"% ;\' aria-valuenow=\"25\" aria-valuemin=\"0\" aria-valuemax=\"100\">"+percent+"%</div>\r\n" + 
						"</div>";
				
				item.setProgress(msg);	
			}			
		}
		
		
		pageInfo.setList(list);
		
	}










	@Override
	public VoteTopic findQuestion(Integer id) {
		// TODO Auto-generated method stub
		
		
		
		return voteMapper.findQuestion(id);
	}

	@Override
	public Integer showCount(Integer id, Integer tid) {
		// TODO Auto-generated method stub
		return voteMapper.findCount(id,tid);
	}

	@Override
	public Integer updateOne(VoteItem item) {
		// TODO Auto-generated method stub
		return voteMapper.updateOne(item);
	}
	

}
